package com.zhiying.crawler.multiThreadClaw;

import com.zhiying.crawler.dao.BookDao;
import com.zhiying.crawler.dao.ChapterDao;
import com.zhiying.crawler.dto.BookDTO;
import com.zhiying.crawler.dto.ChapterDTO;
import com.zhiying.crawler.dto.SortDTO;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@Component
public class BookClawer {
    private static final Logger logger = LoggerFactory.getLogger(SortClawer.class);

    @Autowired
    private BookDao bookDao;
    @Autowired
    private ChapterDao chapterDao;


    @Scheduled(cron = "0 12 15 * * ?")
    public void processBookLinkFromSort(){
        List<SortDTO> sortLinks = bookDao.getinExistSortLink();
        for(SortDTO sortDTO : sortLinks){
            List<BookDTO> books = ClawUtil.getAllLinksPerSort(sortDTO.getSortPage(),sortDTO.getSortLink());
            bookDao.saveBookInfo(books);
        }
        List<BookDTO> untrackBooks = bookDao.getUntrackBooks();
        HttpResponse response = ClawUtil.mockLoginBbdus();
        /*暂且先做此判断，后期优化增加登录不成功重试机制*/
        if(response == null){
            logger.error("mock login to get cookie error");
            return;
        }
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(50);
        BlockingQueue<Map<String,String>> queue = new LinkedBlockingQueue<>(50);
        for(BookDTO bookDTO : untrackBooks){
            ses.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    try {
                        Map<String,String> bookMap = ClawUtil.accessByCookie2(response,bookDTO.getBookLink());
                        queue.put(bookMap);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },0,4,TimeUnit.SECONDS);
        }

        for(BookDTO bookDTO : untrackBooks){
            try {
                Thread.sleep(6000);
                Map<String,String> htmlMap = queue.take();
                for(Map.Entry<String,String> map : htmlMap.entrySet()){
                    String bookLink = map.getKey();
                    String bookHtml = map.getValue();
                    bookDao.updateBookContent(bookHtml,bookLink);

                    BookDTO book = bookDao.getBookByBookLink(bookLink);
                    //处理chapter
                    List<ChapterDTO> chapters = ClawUtil.getAllLinksPerBook(book);
                    chapterDao.saveChapterInfo(chapters);
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        ses.shutdown();
        System.out.println("线程池关闭");

    }
}
